home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / hypercrd / xcmd / sprtxtrn.sea / Support Tools eXternals 1.2.5 / card_3187.txt < prev    next >
Text File  |  1990-11-13  |  12KB  |  366 lines

  1. -- card: 3187 from stack: in.5
  2. -- bmap block id: 7662
  3. -- flags: 0000
  4. -- background id: 3858
  5. -- name: ApplSize
  6. ----- HyperTalk script -----
  7. on hideObjects
  8.   hide cd btn "Try it!"
  9. end hideObjects
  10.  
  11. on showObjects
  12.   show cd btn "Try it!"
  13. end showObjects
  14.  
  15.  
  16. -- part 1 (button)
  17. -- low flags: 00
  18. -- high flags: A002
  19. -- rect: left=82 top=185 right=219 bottom=175
  20. -- title width / last selected line: 0
  21. -- icon id / first selected line: 0 / 0
  22. -- text alignment: 1
  23. -- font id: 0
  24. -- text size: 12
  25. -- style flags: 8192
  26. -- line height: 16
  27. -- part name: Try it!
  28. ----- HyperTalk script -----
  29. on mouseUp
  30.   global errGlobal
  31.   put FilePath("APPL", "Choose an application please.") into fileName
  32.   if fileName = empty then exit mouseUp
  33.   put ApplSize(fileName, "nodialog:errGlobal") into sizes
  34.   if errGlobal Γëá empty then
  35.     answer "Error: ΓÇ£" & errGlobal & "ΓÇ¥"
  36.     put empty into errGlobal
  37.   else
  38.     answer "The application ΓÇ£" & fileName & "ΓÇ¥ is currently set" && "to use" && item 1 of sizes div 1024 & "K, and requires" && "at least" && item 2 of sizes div 1024 & "K."
  39.   end if
  40. end mouseUp
  41.  
  42.  
  43.  
  44.  
  45. -- part contents for background part 20
  46. ----- text -----
  47.      XFCN to return the suggested memory size (in bytes) of the application specified by pathName.  It returns a comma delimited list with the current size setting in item 1, and the minimum size setting in item 2.  
  48.  
  49.      Calling Syntax: AppleSize(pathName,┬½ΓÇ£nodialogΓÇ¥:errorGlobal┬╗)
  50. PATHNAME : the full path name to the file
  51.  
  52. Note: Occasionally, you will get a value for the minimum size that differs from the Finder's Get Info window.  This is because we are reading the true "Minimum" value set by the application and not the "Suggested" value, which is what the Finder uses.
  53.  
  54. -- part contents for background part 38
  55. ----- text -----
  56. 4/50
  57.  
  58. -- part contents for background part 42
  59. ----- text -----
  60. { ApplSize(Pathname)                      }
  61. { XFCN to return the suggested memory size of the       }
  62. { application specified by pathName.  Looks for current size   }
  63. { setting or returns minimum size currently set.  For some  }
  64. { reason, the minimum memory value is not always correct.  }
  65. {                                  }
  66. {}
  67. {}
  68. {  Written by:  Anup Murarka    Eric Carlson              }
  69. {        ALINK:  SKEPTIC    ALINK:  cyNic            }
  70. {                  CIS:  76004,3356}
  71. {}
  72. {        We are part of the Support Tools Development Group,                }
  73. {        Apple Computer, Inc.                                    }
  74. {}
  75. {        please DO NOT contack Mac DTS for support of this code!                }
  76. {}
  77. {        please DO contact the authors for support of this code!                }
  78. {}
  79. {        Send comments, bug reports, requests to any of the above              }
  80. {        E-mail addresses or to:                                  }
  81. {}
  82. {              (one of us)                                  }
  83. {              Apple Computer, Inc.                              }
  84. {              900 E. Hamilton, Ave.                            }
  85. {              Campbell, CA   95008                            }
  86. {              M/S 72-L                                    }
  87. {}
  88. {  Copyright:  ┬⌐ 1989, 1990 by Apple Computer, Inc., all rights reserved.          }
  89. {}
  90. { written by  : Anup Murarka                                      }
  91. { AppleLink  : Skeptic                                          }
  92. { modification history                                           }
  93. {       Date      Initials                  Comments                  }
  94. {       ----      ------    --------------------------------------------------------  }
  95. {    8/14/89        akm    first written                                }
  96. {    8/29/90        ec      removed upper case converion for A/UX compatibility.  add  }
  97. {                     StripAddress Changed version to 1.1                }
  98. {}
  99.  
  100. unit DummyUnit;
  101.  
  102. interface
  103.  
  104.   uses
  105.     HyperXCmd;
  106.  
  107.   procedure MAIN (paramPtr: XCmdPtr);
  108.  
  109. implementation
  110.  
  111.   procedure ApplSize (paramPtr: XCmdPtr);
  112.   FORWARD;
  113.  
  114.   procedure MAIN (paramPtr: XCmdPtr);
  115.   begin
  116.     ApplSize(paramPtr);
  117.   end;
  118.  
  119.   procedure reportToUser (paramPtr: XCmdPtr;
  120.                   msgStr: str255);
  121. {}
  122. { report something back to the user.  }
  123. { the last parameter (optional) to an external may contain }
  124.  { "noDialog" or "noDialog:GlobalName".  GlobalName is the name }
  125.  { of a HyperTalk global variable into which error messages will be }
  126.  { placed.  we've decided to use this approach to avoid confusing }
  127. { an error message with a valid result being returned from an XFCN. }
  128. {}
  129.     var
  130.       tempStr: str255;
  131.   begin
  132. {check the last param to see if the user requested that}
  133. { we suppress the error dialog }
  134.     ZeroToPas(paramPtr, paramPtr^.params[paramPtr^.paramCount]^, tempStr);
  135.     UprString(tempStr, true);
  136.     if pos('NODIALOG', tempStr) = 0 then
  137.   { no special error handling specified, throw up a dialog and return the error message }
  138.       begin
  139.         SendCardMessage(paramPtr, concat('answer "', msgStr, '"'));
  140.         paramPtr^.returnValue := PasToZero(paramPtr, msgStr);
  141.       end
  142.     else if (pos(':', tempStr) > 0) then
  143.   { requested global AND noDialog so we fill in the global and return empty }
  144.       begin
  145.         tempStr := copy(tempStr, pos(':', tempStr) + 1, length(tempStr));
  146.                             { get the name of the HC global  to fill }
  147.         SetGlobal(paramPtr, tempStr, PasToZero(paramPtr, msgStr));
  148.                             { and fill it }
  149.         paramPtr^.returnValue := PasToZero(paramPtr, '');  { return empty }
  150.       end
  151.     else
  152.   { requested noDialog only so we return the error condition as the result }
  153.       paramPtr^.returnValue := PasToZero(paramPtr, msgStr);
  154.   end;  { procedure }
  155.  
  156.   function AskedForHelp (paramPtr: XCmdPtr;
  157.                   syntaxMsg: Str255;
  158.                   copyrightMsg: Str255): boolean;
  159. {  check to see if the user sent a '?' or a '!' as }
  160. { the only parameter. if so we will respond with }
  161. { the calling syntax or the copyright/version info }
  162. { for this external }
  163. {}
  164.     var
  165.       firstStr: str255;
  166.   begin
  167.     askedForHelp := false;
  168.     if paramPtr^.paramCount = 1 then
  169.       begin
  170.         ZeroToPas(paramPtr, paramPtr^.params[1]^, firstStr);
  171.           { what is the first param? }
  172.         if firstStr = '?' then
  173.           begin
  174.             reportToUser(paramPtr, syntaxMsg);
  175.             askedForHelp := true
  176.           end  { asked for help }
  177.         else if firstStr = '!' then
  178.           begin
  179.             reportToUser(paramPtr, copyRightMsg);
  180.             askedForHelp := true
  181.           end;  { asked for copyright info }
  182.       end;  { one parameter passed }
  183.   end;  { function }
  184.  
  185.   function LongToString (paramPtr: XCmdPtr;
  186.                   num: LONGINT): Str255;
  187. { why, oh why did dan write this one as a procedure??? }
  188.     var
  189.       tempStr: str255;
  190.   begin
  191.     LongToStr(paramPtr, num, tempStr);
  192.     LongToString := tempStr;
  193.   end;
  194.  
  195.   function NumberToString (paramPtr: XCmdPtr;
  196.                   num: LONGINT): Str255;
  197. { use the toolbox call rather than HC's }
  198.     var
  199.       tempStr: str255;
  200.   begin
  201.     NumToString(num, tempStr);
  202.     NumberToString := tempStr;
  203.   end;
  204.  
  205.   procedure reportResError (paramPtr: XCmdPtr;
  206.                   errorNum: integer);
  207.     var
  208.       errMsg, tempName: str255;
  209.   begin
  210.     case errorNum of          { what caused the problem? }
  211.       -0: 
  212.         errMsg := 'no error.';
  213.       -36: 
  214.         errMsg := 'I/O Error.';
  215.       -37: 
  216.         errMsg := 'bad file name or volume name.';
  217.       -38: 
  218.         errMsg := 'file not open.';
  219.       -39: 
  220.         errMsg := 'that file has no resource fork.';
  221.       -42: 
  222.         errMsg := 'too many files open.';
  223.       -43: 
  224.         errMsg := 'file not found.';
  225.       -45, -54, -61: 
  226.         errMsg := 'file locked.';
  227.       -47, -49: 
  228.         errMsg := 'file is busy.';
  229.       -53: 
  230.         errMsg := 'that volume is not on line.';
  231.       -108: 
  232.         errMsg := 'not enough room in heap zone.';
  233.       -120: 
  234.         errMsg := 'directory not found.';
  235.       -121: 
  236.         errMsg := 'too many working directories open.';
  237.       -127: 
  238.         errMsg := 'internal file system error.';
  239.       -192: 
  240.         errMsg := 'resource not found.';
  241.       -193: 
  242.         errMsg := 'file not found.';
  243.       otherwise
  244.         errMsg := concat('unexpected error #', NumberToString(paramPtr, errorNum));
  245.     end;    { case }
  246.  
  247.     errMsg := concat('Sorry, ', errMsg);
  248.     reportToUser(paramPtr, errMsg);
  249.     { return the error message }
  250.   end;    { function }
  251.  
  252.   function getParams (paramPtr: XCmdPtr;
  253.                   var inputFile: str255): boolean;
  254.   { function to get the parameters and validate them.  Returns boolean}
  255.   { instructing the main procedure to continue if the parameters passed}
  256.   { are valid.  Also returns syntax messages if requested by the user.}
  257.     var
  258.       numParams: integer;
  259.       inputCh: str255;
  260.       syntaxStr, copyrightStr: str255;
  261.   begin
  262.     getParams := true;  {Initially, assume the parameters are valid.}
  263.     syntaxStr := 'ApplSize(pathname ┬½, ΓÇ£nodialog:ΓÇ¥errorGlobal┬╗)';
  264.     copyrightStr := '┬⌐ 1989, 1990 Apple Computer, Inc., v.1.1, by Anup Murarka';
  265.  
  266.             {check that we have the proper number of parameters}
  267.     numParams := paramPtr^.paramCount;
  268.     if (numParams < 1) or (numParams > 2) then
  269.       begin
  270.         getParams := false;
  271.         reportToUser(paramPtr, syntaxStr);
  272.         exit(GetParams);
  273.       end;
  274.             { asking for help or copyright info? }
  275.     if AskedForHelp(paramPtr, syntaxStr, copyrightStr) then
  276.       begin
  277.         getParams := false;
  278.         exit(getParams);
  279.       end;
  280.  
  281.     { convert HyperCard's zero terminated string to a Pascal string}
  282.     ZeroToPas(paramPtr, paramPtr^.Params[1]^, inputFile);
  283.   end;
  284.  
  285.   function curResLoad: boolean;
  286.   { return the current setting of the system 'ResLoad'.  Are resources }
  287.   { actually loaded or not?}
  288.     const
  289.       ResLoad = $A5E;      { determines whether or not resources are pre-loaded}
  290.     type
  291.       booleanPtr = ^boolean;
  292.   begin
  293.     curResLoad := booleanPtr(ResLoad)^;
  294.   end;
  295.  
  296.   procedure ApplSize (paramPtr: XCmdPtr);
  297.     label
  298.       10, 20;
  299.     type
  300.       SIZEptr = ^SIZErec;
  301.       SIZErec = record
  302.           bkgndFlags: integer;
  303.           MemorySize: longint;
  304.           MinMemSize: longint;
  305.         end;
  306.     var
  307.       paramsOK, oldResLoad: boolean;
  308.       oldCurResFile: integer;
  309.       WDVRefNum: integer;
  310.       inputFile: str255;
  311.       inputRefNum: integer;
  312.       DirID: longint;
  313.       wdParams: WDPBRec;
  314.       error: OSErr;
  315.       SIZErecHndl: handle;
  316.       inputSIZE: SIZEptr;
  317.       CurrentSize, MinimumSize, result: str255;
  318.  
  319.   begin
  320.     paramsOK := getParams(paramPtr, inputFile);
  321.     if paramsOK = false then
  322.       exit(ApplSize);
  323.  
  324.     oldResLoad := curResLoad;
  325.     oldCurResFile := curResFile;
  326.  
  327.     SetResLoad(FALSE);   { avoid slow preloads }
  328.     inputRefNum := OpenRFPerm(StringPtr(StripAddress(@inputFile))^, 0, fsRdPerm);
  329.     error := resError;
  330.     if error <> noErr then
  331.       begin
  332.         reportResError(paramPtr, error);
  333.         goto 10;
  334.       end;
  335.     SetResLoad(TRUE);
  336.  
  337.     SIZErecHndl := Get1Resource('SIZE', 0);
  338.     if SIZErecHndl = nil then
  339.       begin
  340.     { if 'SIZE' 0 is not available, then try 'SIZE' -1}
  341.         SIZErecHndl := Get1Resource('SIZE', -1);
  342.         if SIZErecHndl = nil then
  343.           begin
  344.             reportToUser(paramPtr, 'Could not load ΓÇÿSIZEΓÇÖ resources');
  345.             goto 20;
  346.           end;
  347.       end;
  348.  
  349.     MoveHHi(SIZErecHndl);
  350.     HLock(SIZErecHndl);
  351.     inputSIZE := SIZEptr(SIZErecHndl^);
  352.     CurrentSize := LongToString(paramPtr, inputSIZE^.MemorySize);
  353.     MinimumSize := LongToString(paramPtr, inputSIZE^.MinMemSize);
  354.     HUnLock(SIZErecHndl);
  355.     ReleaseResource(SIZErecHndl);
  356.     result := concat(CurrentSize, chr(44), MinimumSize);
  357.     paramPtr^.returnValue := PasToZero(paramPtr, result);
  358.  
  359. 20:
  360.     CloseResFile(inputRefNum);
  361. 10:
  362.     SetResLoad(oldResLoad);
  363.     UseResFile(oldCurResFile);
  364.   end;
  365.  
  366. end.